package questionary;

import java.text.DecimalFormat;

import myExceptions.MyFileException;

/**
 * 
 * Clase que contiene la respusta de las preguntas de tipo 2, hereda los metodos 
 * y la informacion de la clase Question
 * @see Questionary
 */
public class NumberQ extends Question { 
	private Double answer;

	/**
	 * Construye una pregunta de tipo 2
	 * @param question La pregunta
	 * @param answer La respuesta
	 * @param diffic La dificultad
	 * @param score El puntaje
	 * @throws MyFileException Si no se pudo crear correctamente
	 */
	public NumberQ(String question, String answer, int diffic, int score) throws MyFileException {
		super(2, diffic, score, question);
		try {
			this.answer = Double.parseDouble(answer);			
		} catch (NumberFormatException e) {
			throw new MyFileException("Wrong number format");
		}
		
	}
	
	/**
	 * Construye una pregunta de tipo 2
	 * @param question La pregunta
	 * @param answer La respuesta
	 * @param diffic La dificultad
	 * @param score El puntaje
	 * @throws MyFileException Si nose pudo crear correctamente
	 */
	public NumberQ(String question, Number answer, int diffic, int score) throws MyFileException {
		super(2, diffic, score, question);
		this.answer = answer.doubleValue();
	}

	@Override
	public int getOptionsCount() {
		return 1;
	}

	@Override
	public String[] getOptions() {
		String [] ret = new String [1];
		ret[0] = answer.toString();
		return ret;
	}

	@Override
	public boolean isCorrect(String[] s) {
		Double aux;
		try {
			if(s[0] != null) 
				aux = Double.parseDouble(s[0]);
			else 
				return false;
		} catch (NumberFormatException e) {
			return false;
		}
		return roundFourDecimals(answer).equals(roundFourDecimals(aux));
	}

	@Override
	public boolean isCorrect(String s) {
		Double aux;
		try {
			aux = Double.parseDouble(s);
		} catch (NumberFormatException e) {
			return false;
		}
		//TODO
		System.out.println("mmm\naux"+roundFourDecimals(aux));
		System.out.println("mmm\nans"+roundFourDecimals(answer));
		
		return roundFourDecimals(answer).equals(roundFourDecimals(aux));
	}
	
	/**
	 * @return True si se contesto correctamente la pregunta
	 */
	public boolean isCorrect(Number n) {
		return roundFourDecimals(answer).equals(roundFourDecimals(n.doubleValue()));
	}
	
	@Override
	public boolean isCorrect(String[] s, int answsize) {
		return false;
	}

	@Override
	public String[] getAnswers() {
		String [] ret = new String [1];
		ret[0] = answer.toString();
		return ret;
	}
	
	@Override
	public int getCorrectOptionsCount() {
		return 1;
	}

	@Override
	public void setOptions(String[] array) {
		return;
	}

	@Override
	public void setAnswers(String[] array) throws MyFileException {
		try {
			this.answer = Double.parseDouble(array[0]);
		} catch (NumberFormatException e) {
			throw new MyFileException("Answer format incorrect");
		}
		
	}
	
	/**
	 * Convierte un numero de tipo double redondeandolo a 4 cifras decimales
	 * @param d El numero a redondear
	 * @return String de un numero redondeado a 4 cifras decimales
	 */
	private String roundFourDecimals(double d) {
    	DecimalFormat fourDForm = new DecimalFormat("#.####");
    	return fourDForm.format(d);
	}
}
